package demo1.servlet.userMai;

import com.alibaba.fastjson.JSON;
import demo1.pojo.goods;
import demo1.pojo.goods_type;
import demo1.pojo.users;
import demo1.service.goodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/showAllGoodsType")
public class showAllGoodsType extends HttpServlet {//展示所有商品种类
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset = utf-8");

        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        users users = (users) userObject;

        List<goods_type> goods_typeList;
        try {
            goodsService show = new goodsService();
            goods_typeList = show.showAllGoodsType();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String jsonString = JSON.toJSONString(goods_typeList);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
